home *** CD-ROM | disk | FTP | other *** search
- /*
- Task Manager -- Background processing support
- version 2.2
-
- This software source package is Copyright ⌐ 1990-91 by Michael Hecht. All Rights
- Reserved. It may be freely distributed in source or object code format; however,
- the source code may not be sold for profit or charged for in any way. The source
- code must be distributed as a package including all H files, sample code and
- projects, and documentation.
-
- I welcome any comments or suggestions that will help me improve or extend the
- functionality of the Task Manager. You can reach me at:
-
- Internet: Michael_Hecht@mac.sas.com
- AppleLink: SAS.HECHT
-
- Happy Tasking!
-
-
- --Michael Hecht
- */
-
- #include "Task.h"
-
- typedef struct {
- Rect portRect;
- StringPtr title;
- StringPtr msg1, msg2;
- WindowPtr slaveWindow;
- } SlaveRecord, **SlaveHandle;
-
- /* Prototypes */
- static void InitToolBox( void );
- static OSErr NewSlave( Rect *bounds, StringPtr title, StringPtr msg1, StringPtr msg2 );
- static void SlaveProc( long taskRefCon );
- static void SlaveTermProc( long taskRefCon );
- static void DrawRandomMsg( WindowPtr theWindow, StringPtr msg1, StringPtr msg2 );
- void main( void );
-
-
- static void InitToolBox( void )
- {
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0 );
- FlushEvents( everyEvent, 0 );
- InitCursor();
- }
-
- static OSErr NewSlave( Rect *bounds, StringPtr title, StringPtr msg1, StringPtr msg2 )
- {
- OSErr err;
- SlaveRecord theSlave;
- SlaveHandle theSlaveHandle;
-
-
- /* Create a new slave; first, initialize a SlaveRecord */
- theSlave.portRect = *bounds;
- theSlave.title = title;
- theSlave.msg1 = msg1;
- theSlave.msg2 = msg2;
-
- /* Be sure to set this to 0, in case the term proc gets called too early! */
- theSlave.slaveWindow = 0;
-
- /*
- * Next; convert it to a SlaveHandle, so it can sit in the heap until the
- * slave can retrieve it.
- */
- err = PtrToHand( &theSlave, ( Handle * )&theSlaveHandle,
- sizeof( SlaveRecord ));
- if( err != noErr )
- return err;
-
- /* Create the slave task, using the SlaveHandle as its taskRefCon */
- err = NewTask( SlaveProc, SlaveTermProc, ( long )theSlaveHandle, 0 );
- if( err != noErr )
- DisposHandle(( Handle )theSlaveHandle );
-
- return err;
- }
-
- static void SlaveProc( long taskRefCon )
- {
- SlaveHandle theSlaveHandle;
- SlaveRecord theSlave;
- WindowPtr slaveWindow;
-
-
- /* Our refCon is really a SlaveHandle; get the record it points to */
- theSlaveHandle = ( SlaveHandle )taskRefCon;
- theSlave = **theSlaveHandle;
-
- /* Create a window for our slave */
- slaveWindow = NewWindow( 0, &theSlave.portRect, theSlave.title,
- TRUE, noGrowDocProc, FrontWindow(), TRUE, 0 );
-
- /* Store the window ptr in our SlaveHandle so the TaskTerm procedure can find it */
- ( *theSlaveHandle )->slaveWindow = slaveWindow;
-
- /* Draw messages forever (the main task will stop us when it's time) */
- for( ;; ) {
-
- /* Let other tasks run */
- TaskYield();
-
- /* Draw one of our messages */
- DrawRandomMsg( slaveWindow, theSlave.msg1, theSlave.msg2 );
- }
- }
-
- static void SlaveTermProc( long taskRefCon )
- {
- SlaveHandle theSlaveHandle;
- WindowPtr slaveWindow;
-
-
- /* Our refCon is really a SlaveHandle */
- theSlaveHandle = ( SlaveHandle )taskRefCon;
-
- /* Time to close our window (if it was ever created) */
- slaveWindow = ( *theSlaveHandle )->slaveWindow;
- if( slaveWindow )
- CloseWindow( slaveWindow );
-
- /* SlaveHandle no longer needed */
- DisposHandle(( Handle )theSlaveHandle );
- }
-
- static void DrawRandomMsg( WindowPtr wp, StringPtr msg1, StringPtr msg2 )
- {
- Rect r;
- RgnHandle scrollRgn;
-
-
- SetPort( wp );
-
- /* Scroll the screen */
- scrollRgn = NewRgn();
- r = wp->portRect;
- ScrollRect( &r, 0, -16, scrollRgn );
- FillRgn( scrollRgn, white );
- DisposeRgn( scrollRgn );
-
- /* Pick one of two messages at random and draw it */
- MoveTo( 5, 128 );
- DrawString(( Random() & 1 ) ? msg1 : msg2 );
- }
-
-
- void main( void )
- {
- OSErr err;
- WindowPtr masterWindow, theWindow;
- Rect r;
- Boolean timeToQuit;
- EventRecord theEvent;
-
-
- /* Initialize the ToolBox */
- InitToolBox();
-
- /* Turn on tasking */
- err = InitTasking();
- if( err != noErr )
- return;
-
- /* Make slaves */
- SetRect( &r, 261, 45, 507, 176 );
- err = NewSlave( &r, "\pTask 1",
- "\pSee my Task 1 message?", "\pOf course you do╤I╒m Task 1!" );
- if( err != noErr )
- return;
-
- SetRect( &r, 5, 206, 251, 337 );
- err = NewSlave( &r, "\pTask 2",
- "\pTHIS is Task 2?", "\pY E S, it is!" );
- if( err != noErr )
- return;
-
- SetRect( &r, 261, 206, 507, 337 );
- err = NewSlave( &r, "\pTask 3",
- "\pThe great and mighty Task 3.", "\pSee me??? I╒m Task 3!" );
- if( err != noErr )
- return;
-
- /* Make master window */
- SetRect( &r, 5, 45, 251, 176 );
- masterWindow = NewWindow( 0, &r, "\pMaster Task", TRUE, noGrowDocProc,
- ( WindowPtr )-1, TRUE, 0 );
-
- /* A very simple event loop */
- timeToQuit = FALSE;
- do {
- if( WaitNextEvent( everyEvent, &theEvent, 0, 0 )) {
-
- switch( theEvent.what ) {
-
- case mouseDown:
- switch( FindWindow( theEvent.where, &theWindow )) {
-
- case inGoAway:
- /* Track it */
- if( !TrackGoAway( theWindow, theEvent.where ))
- break;
-
- /* It's time to quit if they close the master window */
- timeToQuit = ( masterWindow == theWindow );
- break;
-
- case inContent:
- case inDrag:
- /* Bring it to the front if need be */
- if( theWindow != FrontWindow())
- SelectWindow( theWindow );
- break;
- }
- break;
-
- case updateEvt:
- /* Clear out any update event */
- theWindow = ( WindowPtr )theEvent.message;
- BeginUpdate( theWindow );
- EndUpdate( theWindow );
- break;
- }
- }
-
- /* Allow tasks to run for five ticks */
- RunTasks( 5 );
-
- /* Let the master task do something useful */
- DrawRandomMsg( masterWindow,
- "\pObey me╤I am the Master!", "\pClose me to quit." );
-
- /* Continue until it's time to quit or there are no more tasks */
- } while( !timeToQuit );
-
- /* Close the master window */
- CloseWindow( masterWindow );
-
- /*
- * Don't forget to do this! It will dispose of all tasks,
- * thereby calling each one's TaskTerm procedure.
- */
- TermTasking();
- }